<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    // rest 参数
    // 用来代替arguments
    // 1. rest 参数必须是函数的最后一个参数，不然
    // 2. ...rest 是一个数组
    function sum(...rest) {
      console.log(rest);
      let total = 0;
      rest.forEach(item => {
        total += item;
      })
      return total;
    }
    console.log(sum(1, 2, 3, 4, 5));     // [1,2,3,4,5]

    // 3. rest 参数可以和普通参数一起使用
    function sum(a, b, ...rest) {
      console.log(a, b, rest);
      let total = a + b;
      rest.forEach(item => {
        total += item;
      })
      return total;
    }
    console.log(sum(1, 2, 3, 4, 5));     // 1 2 [3,4,5]
  </script>

</body>

</html>